package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.ISalaryBaseRelevanceDao;
import com.boss.salary.entity.SalaryBaseRelevanceDTO;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
@Repository
public class SalaryBaseRelevanceDaoImpl extends BaseDaoImpl implements ISalaryBaseRelevanceDao {
    @Override
    public List<SalaryBaseRelevanceDTO> selectSalaryBaseRelevance(PaginationDTO paginationDTO, String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select b.id,b.create_time, a.id base_salary_id,a.salary_code base_salary_code, a.salary_name base_salary_name,b.city_salary_code_list,city_salary_name_list,b.is_deleted from ")
        .append(TableName.SALARY_BASEDATA).append(" a ").append("left join ").append(TableName.SALARY_BASE_RELEVANCE).append(" b ")
        .append(" on  a.id=b.base_salary_id and b.mof_div_code=? where a.mof_div_code='220000000' and a.is_deleted=? and a.is_show=? order by a.show_order ");
        Object[] objects=new Object[]{
                province, SystemDictConstant.NOT_DELETED,SystemDictConstant.IS_TRUE
        };
        String querySql = boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = boxUpParam(objects, paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(SalaryBaseRelevanceDTO.class), params);
    }

    @Override
    public int selectSalaryBaseRelevanceTotal(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ")
                .append(TableName.SALARY_BASEDATA).append(" a ").append("left join ").append(TableName.SALARY_BASE_RELEVANCE).append(" b ")
                .append(" on  a.id=b.base_salary_id and b.mof_div_code=? ");
//                        "where a.mof_div_code='220000000' and a.is_deleted=? and a.is_show=? order by a.show_order ");
        Object[] objects=new Object[]{
                province, SystemDictConstant.NOT_DELETED,SystemDictConstant.IS_TRUE
        };
            return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,objects);


    }

    @Override
    public void mergeIntoSalaryBaseRelevance(List<SalaryBaseRelevanceDTO> srList) {

        StringBuilder sb = new StringBuilder();

        sb.append("MERGE into ").append(TableName.SALARY_BASE_RELEVANCE);
        sb.append(" t using (select ? s.id,? s.create_time,? s.update_time,? s.base_salary_id,")
                .append("? s.base_salary_code,? s.base_salary_name,? s.city_salary_code_list,? city_salary_name_list,")
                .append(" ? s.is_salary_submit,? s.is_person_submit,? s.is_agency_submit,? s.is_confirm_submit,? s.is_deleted,? s.mof_div_code,")
                .append("  from sal_salary_base_relevance ) s on (t.id=s.id)")
                .append(" when matched then update set " )
                .append(" ( t.id=s.id,t.create_time=s.create_time ,t.update_time=s.update_time,t.base_salary_id=s.base_salary_id, t.base_salary_code=s.base_salary_code, " )
                .append("t.base_salary_name =s.base_salary_name, t.city_salary_code_list=s.city_salary_code_list, t.city_salary_name_list=s.city_salary_name_list,")
                .append("t.is_deleted=s.is_deleted , t.mof_div_code=s.mof_div_code ) " )
                .append(" when not matched then insert values " )
                .append("s.id,s.create_time ,s. update_time, s.base_salary_id, base_salary_code, " )
                .append("s.base_salary_name, s.city_salary_code_list,s.city_salary_name_list,")
                .append("s.is_deleted ,s.mof_div_code " );
        List<Object[]> paramsList = new ArrayList<>(srList.size());
        Timestamp datetime=DateUtils.getCurrentTimeStamp();
        for (SalaryBaseRelevanceDTO sr : srList) {
            if(SysUtils.isNullOrEmpty(sr.getId())) {
                Object[] params = new Object[]{
                        SysUtils.generateGuid(), datetime, datetime,
                        sr.getBaseSalaryId(),sr.getBaseSalaryCode(),sr.getBaseSalaryName(),
                        sr.getCitySalaryCodeList()==null?"":sr.getCitySalaryCodeList(),
                        sr.getCitySalaryNameList()==null?"":sr.getCitySalaryNameList(),
                        sr.getIsDeleted(),sr.getMofDivCode()
                };
                paramsList.add(params);
            }else{
                Object[] params = new Object[]{
                        sr.getId(), sr.getCreateTime(),datetime,
                        sr.getBaseSalaryId(),sr.getBaseSalaryCode(),sr.getBaseSalaryName(),
                        sr.getCitySalaryCodeList()==null?"":sr.getCitySalaryCodeList(),
                        sr.getCitySalaryNameList()==null?"":sr.getCitySalaryNameList(),
                        sr.getIsDeleted(),sr.getMofDivCode()
                };
                paramsList.add(params);
            }
        }
        super.getjdbcTemplate().batchUpdate(sb.toString(), new BatchPreparedStatementSetter() {
            public int getBatchSize() {
                return paramsList.size();
                //这个方法设定更新记录数，通常List里面存放的都是我们要更新的，所以返回list.size();
            }
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                Object[] sme = paramsList.get(i);
                for(int a = 0; a < sme.length; a++){
                    ps.setObject(a+1,sme[a]);
                }

            }
        });
    }
}
